package com.efounder.frame.utils;

import android.app.Activity;
import androidx.fragment.app.Fragment;
import android.util.Log;

import com.core.xml.StubObject;
import com.efounder.frame.fragment.EFDefaultFragment;
import com.efounder.frame.xmlparse.EFXmlViewTypeParser;
import com.efounder.util.AppContext;

/**
 * Created by hudq on 2016/8/24. XX
 */

public class EFViewTypeUtils {

    /***
     * 根据ViewType获取Fragment
     * @param viewType
     * @return
     */
    public static Fragment getFragment(String viewType){
        Fragment fragment = null;
        String fragmentClassName = getClassName(viewType);
        if (fragmentClassName == null){
            return new EFDefaultFragment();
        }
        try {
            Class clazz = Class.forName(fragmentClassName);
            if (Fragment.class.isAssignableFrom(clazz)){//clazz是Fragment的子类
                fragment = (Fragment) clazz.newInstance();
            }else {
                Log.e("--","view_type.xml中viewType=" + viewType + ",类："+fragmentClassName + "不是Fragment（V4）实例");
                //创建默认的Fragment
                fragment = new EFDefaultFragment();
            }
        } catch (Exception e) {
            //e.printStackTrace();
            Log.e("--","view_type.xml中viewType=" + viewType + ",没有找到类："+fragmentClassName);
            //创建默认的Fragment
            fragment = new EFDefaultFragment();
        }
        return fragment;
    }


    public static Class<Activity> getActivityClass(String viewType){
        Class<Activity> activityClass = null;
        String activityClassName = getClassName(viewType);
        if (activityClassName == null){
            return null;
        }
        try {
            Class clazz = Class.forName(activityClassName);
            if (Activity.class.isAssignableFrom(clazz)){//clazz是否是Activity的子类
                activityClass = clazz;
            }else {
                Log.e("--","view_type.xml中viewType=" + viewType + ",类："+activityClassName + "不是Activity子类");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            Log.e("--","view_type.xml中viewType=" + viewType + ",没有找到类："+ activityClassName);
        }
        return activityClass;
    }

    /**
     *
     * @param viewType
     * @return 返回V4.Fragment的子类，或者Activity子类
     */
    public static Class getActivityOrFragmentClass(String viewType) {
        Class activityOrFragmentClazz = null;
        String className = getClassName(viewType);
        if (className == null){
            return null;
        }
        try {
            Class clazz = Class.forName(className);
            if (Activity.class.isAssignableFrom(clazz) || Fragment.class.isAssignableFrom(clazz)){
                activityOrFragmentClazz = clazz;
            }else{
                Log.e("--","view_type.xml中viewType=" + viewType + ",类："+className + "不是Activity子类,也不是Fragment的子类");
            }
        } catch (ClassNotFoundException e) {
            Log.e("--","view_type.xml中viewType=" + viewType + ",没有找到类："+ className);
        }
        return activityOrFragmentClazz;
    }



    /**
     * 根据ViewType获取对应的类名
     * @param viewType
     * @return
     */
    private static String getClassName(String viewType){
        String className = null;
        StubObject viewTypeStub = EFXmlViewTypeParser.getStubByViewType(AppContext.getInstance(),viewType);
        if (viewTypeStub == null){
            Log.e("--","view_type.xml中没有viewType=" + viewType + "的相关配置！");
        }else {
            className = viewTypeStub.getString("class","");
            if (className.trim().equals("")){
                Log.e("--","view_type.xml中没有viewType=" + viewType + "的class的配置！");
            }
        }
        return className;
    }



}
